package club.ljwang.service;

import club.ljwang.dao.UserNameDao;
import club.ljwang.entity.PageResult;
import club.ljwang.entity.QueryPageBean;
import club.ljwang.pojo.Role;
import club.ljwang.pojo.User;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = UserNameService.class)
@Transactional
public class UserNameServiceImpl implements UserNameService {

    @Autowired
    private UserNameDao userNameDao;
    //分页
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //1.告诉Mybatis我们要分页
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //2.写一个跟分页毫无关系的sql
        List<User> groupList = userNameDao.findPage(queryPageBean.getQueryString());
        for (User user : groupList) {
            Date birthday = user.getBirthday();
            String format = simpleDateFormat.format(birthday);
            user.setStringBirthday(format);
        }
        return new PageResult(page.getTotal(), groupList);
    }

    //查询所有角色
    @Override
    public List<Role> queryall() {
         return userNameDao.queryall();
    }

    //添加
    @Override
    public void add(User user) {
        //插入检查组的信息并返回主键
        userNameDao.add(user);
        //获取主键
        Integer userId = user.getId();
        List<Integer> roleIds = user.getRoleIds();
        setroleIdAnduserIdBath(roleIds,userId);
    }

    public void setroleIdAnduserIdBath(List<Integer> roleIds,Integer userId) {
        if (CollectionUtil.isNotEmpty(roleIds)){
            userNameDao.setroleIdAnduserIdBath(roleIds,userId);
        }
    }

    //弹出编辑框查询
    @Override
    public Map findAll4Edit(Integer id) {
        Map map=new HashMap<>();
        //根据id查询用户
        User user= userNameDao.userById(id);
        //回显所有的角色
        List<Role> queryall = userNameDao.queryall();
        //用户勾选检查项id的集合
        List<Integer> findRoleByIds = userNameDao.findRoleById(id);
        map.put("user",user);
        map.put("queryall",queryall);
        map.put("findRoleByIds",findRoleByIds);
        return map;
    }

    //编辑
    @Override
    public void enit(User user) {
        //根据id修改检查组的基本信息
        userNameDao.enit(user);
        //删除原来的关系
        userNameDao.deleteAssociation(user.getId());
        //建立新的关系
        setroleIdAnduserIdBath(user.getRoleIds(),user.getId());
    }

    //删除
    @Override
    public void delete(Integer id){
        Integer count = userNameDao.findCountByCheckItemId(id);
        if (count>0){
           userNameDao.deleteUserRole(id);
        }
        userNameDao.delete(id);
    }
}

